package com.bridgeintelligent.tag.customer.service;

import com.bridgeintelligent.tag.customer.entity.AnalysisCatalog;
import com.bridgeintelligent.tag.customer.entity.Result;
import com.bridgeintelligent.tag.customer.mapper.AnalysisCatalogMapper;
import com.bridgeintelligent.tag.user.mgmt.pojo.User;
import com.bridgeintelligent.tag.webserver.security.TagSecurityHelper;
import com.wayneleo.quickstart.framework.IDGenerator;
import lombok.AllArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class AnalysisCatalogServiceImpl implements IAnalysisCatalogService {

    private AnalysisCatalogMapper analysisCatalogMapper;

    @Override
    public List findList() {
        User user = TagSecurityHelper.currentUser();
        List<AnalysisCatalog> result = analysisCatalogMapper.findList(user.getUserId());
        if (CollectionUtils.isEmpty(result)){
            return new ArrayList();
        }
        Map<String, List<AnalysisCatalog>> analysisListMap = result.stream()
                                                         .collect(Collectors.groupingBy(AnalysisCatalog::getPid));
        List<AnalysisCatalog> list = new ArrayList<>(result.size());
        Map<String, AnalysisCatalog> analysisMap = result.stream()
                                               .collect(Collectors.toMap(AnalysisCatalog::getId, analysisCatalog -> analysisCatalog));
        List<AnalysisCatalog> parentList = analysisListMap.get("-");
        parentList.forEach(analysisCatalog -> list.add(findVO(analysisListMap, analysisMap, analysisCatalog.getId())));
        parentList.sort(Comparator.comparing(AnalysisCatalog::getAlName));
        return parentList;
    }

    @Override
    public void addCatalog(AnalysisCatalog catalog) {
        if (StringUtils.isEmpty(catalog.getId())){
            catalog.setId(IDGenerator.gen());
        }
        User user = TagSecurityHelper.currentUser();
        catalog.setCtName(user.getUserId());
        analysisCatalogMapper.addCatalog(catalog);
    }

    @Override
    public void updateCatalog(AnalysisCatalog catalog) {
        analysisCatalogMapper.updateCatalog(catalog);
    }

    @Override
    public Result delCatalog(String id) {
        User user = TagSecurityHelper.currentUser();
        List<AnalysisCatalog> list = analysisCatalogMapper.findNext(user.getUserId(),id);
        if (list.isEmpty()) {
            analysisCatalogMapper.delCatalog(id);
            return new Result(0,"删除成功");
        } else {
            return new Result(-1,"删除失败：存在子目录！");
        }
    }

    @Override
    public boolean checkName(String alName, String pid) {
        User user = TagSecurityHelper.currentUser();
        int count = analysisCatalogMapper.checkName(alName, pid, user.getUserId());
        if(count > 0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public String checkLevel(String pid) {
        User user = TagSecurityHelper.currentUser();
        return analysisCatalogMapper.checkLevel(pid,user.getUserId());
    }

    private AnalysisCatalog findVO(Map<String, List<AnalysisCatalog>> analysisListMap, Map<String, AnalysisCatalog> analysisMap, String id) {
        AnalysisCatalog analysisCatalog = analysisMap.get(id);
        List<AnalysisCatalog> list = analysisListMap.get(analysisCatalog.getId());
        if (null != list) {
            list.sort(Comparator.comparing(AnalysisCatalog::getAlName));
            list.forEach(analysisCatalog1 -> {
                AnalysisCatalog chirld = findVO(analysisListMap, analysisMap, analysisCatalog1.getId());
                analysisCatalog.getChildren()
                        .add(chirld);
            });
        } else {
            return analysisCatalog;
        }
        return analysisCatalog;
    }
}
